import java.util.Arrays;

public class MyArraysList<E extends Comparable<E>> implements SeqList{

    public Object[] arrays;
    public int useCount;
    public static final int capacity = 5;

    public MyArraysList() {
        this.arrays = new Object[capacity];
        this.useCount = 0;
    }

    public MyArraysList(int capacity) {
        this.arrays =  new Object[capacity];
        this.useCount = 0;
    }

    @Override
    public void sort(){
         for (int i = 0; i < useCount - 1; i++) {
             boolean f = true;
             for (int j = 0; j < useCount - 1 - i; j++) {
               if(((E)(arrays[j])).compareTo((E)arrays[j + 1]) > 0){
                   E tem = (E)arrays[j];
                   arrays[j] = arrays[j + 1];
                   arrays[j + 1] = tem;
                   f = false;
               }
             }
             if(f) break;
         }
    }

    private void addCapacity(){
        this.arrays = Arrays.copyOf(arrays,arrays.length * 2);
    }


    @Override
    public void add(Object data) {
        //扩容
         if(useCount == arrays.length){
             addCapacity();
         }
         //增加元素
        arrays[useCount] = (E)data;
         useCount++;
    }

    @Override
    public void add(int pos, Object data) {
        //判断坐标是否合法
        if(pos < 0 || pos > useCount){
            throw new CheckPosException("坐标非法，不可增加  pos:" + pos);
        }
        //扩容
        if(useCount == arrays.length){
            addCapacity();
        }
        //增加元素
        //移动元素
        for (int i = useCount; i > pos  ; i--) {
            arrays[i] = arrays[i - 1];
        }
        arrays[pos] = (E)data;
        useCount++;
    }

    @Override
    public boolean contains(Object toFind) {
        if (isEmpty()) return false;
        for (int i = 0; i < useCount; i++) {
            if(arrays[i] == toFind) return true;
        }
        return false;
    }

    @Override
    public int indexOf(Object toFind) {
        if(isEmpty()) return -1;
        for (int i = 0; i < useCount; i++) {
            if(arrays[i] == toFind) return i;
        }
        return -1;
    }

    @Override
    public E get(int  pos) {
        if(pos < 0 || pos > useCount)
            throw new CheckPosException("坐标非法，不可查找！ pos：" + pos);
        if(useCount == 0){
            System.out.println("数组为空，不可查找！");
            throw new CheckCapacityException("数组容量为空，不可查找！");
        }
        return (E)arrays[pos];
    }

    @Override
    public void set(int pos, Object value) {
        if(checkPos(pos))
            throw new CheckPosException("坐标非法，不可设置！ pos:" + pos);
        if(useCount == 0) throw new CheckCapacityException("数组容量为空，不可设置！ ");

        arrays[pos] = (E)value;
    }

    @Override
    public void remove(Object toRemove) {
          if(isEmpty())
              throw new CheckCapacityException("数组容量为空，不可移除元素！");
          int i = 0;
        for (; i < useCount; i++) {
            if(arrays[i] == toRemove) break;
        }
        if(i == useCount){
            System.out.println("指定移除的元素不存在：" + toRemove);
            return;
        }
        for (;i < useCount - 1;i++){
            arrays[i] = arrays[i + 1];
        }
        useCount--;
        System.out.println(toRemove + "已移除！");
    }

    @Override
    public int size() {
        return  useCount;
    }

    @Override
    public void clear() {
       arrays = null;
    }

    @Override
    public boolean isEmpty() {
        return useCount == 0;
    }

    @Override
    public boolean checkPos(int pos) {
        if(pos < 0 || pos >= useCount )
            return true;
        return false;
    }

    @Override
    public void display() {
        if(isEmpty())
            throw new CheckCapacityException("数组为容量空，不可打印！");
        System.out.print("Arrays = [ ");
        for (int i = 0; i < useCount; i++) {
            if(useCount - 1 == i )
            System.out.println(arrays[i] + " ]");
            else
                System.out.print(arrays[i] + ",");
        }
    }



}
